package com.zxm.service.impl;
/**
 * 张鑫铭
 */

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxm.mapper.UserInfoMapper;
import com.zxm.pojo.UserInfo;
import com.zxm.pojo.UserInfoExample;
import com.zxm.pojo.UserInfoExample.Criteria;
import com.zxm.result.EasyUiResult;
import com.zxm.result.GuhealthResult;
import com.zxm.service.UserInfoService;
import com.zxm.util.ConnectionUtil;
import com.zxm.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceName = "com.zxm.service.UserInfoService")
@Transactional
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    //展示用户信息
    @Override
    public UserInfo showUserInfo(UserInfo userInfo) {
        UserInfoExample ue = new UserInfoExample();
        Criteria c = ue.createCriteria();
        c.andUsernameEqualTo(userInfo.getUsername());
        List<UserInfo> list = userInfoMapper.selectByExample(ue);
        UserInfo u = null;
        if (null != list && list.size() > 0) {
            u = userInfoMapper.selectByPrimaryKey(list.get(0).getId());
        }
        return u;
    }

    //修改用户信息
    @Override
    public void modifyUserInfo(UserInfo userInfo) {
        UserInfoExample ue = new UserInfoExample();
        Criteria c = ue.createCriteria();
        c.andUsernameEqualTo(userInfo.getUsername());
        List<UserInfo> list = userInfoMapper.selectByExample(ue);
        UserInfo u = null;
        if (null != list && list.size() > 0) {
            u = userInfoMapper.selectByPrimaryKey(list.get(0).getId());
            userInfo.setId(u.getId());
            this.userInfoMapper.updateByPrimaryKeySelective(userInfo);
        }
    }

    //用户注册
    @Override
    public int insertUserInfo(UserInfo userInfo) {
        userInfo.setStatus(2);
        return this.userInfoMapper.insertSelective(userInfo);
    }

    /**
     * 分页查询所有用户
     */
    @Override
    public EasyUiResult selectByPage(Integer pageNum, Integer pageSize) {
        //分页
        PageHelper.startPage(pageNum, pageSize);
        UserInfoExample example = new UserInfoExample();
        //id倒叙查询
        example.setOrderByClause("id desc");
        List<UserInfo> goodsList = userInfoMapper.selectByExample(example);
        //封装pageInfo对象
        PageInfo<UserInfo> pageInfo = new PageInfo<>(goodsList);
        //返回结果
        EasyUiResult result = new EasyUiResult();
        result.setRows(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @Override
    public Boolean checkUsername(String username) {
        UserInfoExample ue = new UserInfoExample();
        Criteria c = ue.createCriteria();
        c.andUsernameEqualTo(username);
        UserInfo userInfo = null;
        List<UserInfo> userInfoList = userInfoMapper.selectByExample(ue);
        if (null != userInfoList && userInfoList.size() > 0) {
            userInfo = userInfoList.get(0);
        }
        if (userInfo == null) {
            return true;
        }
        return false;
    }

    @Override
    public Map<String, Integer> getUserInfoInTenDa(Date regTime) {
        Date date = DateUtil.getDateBySelect(regTime, 10);
        UserInfoExample example = new UserInfoExample();
        example.createCriteria().andCreateTimeGreaterThanOrEqualTo(regTime);
        example.createCriteria().andCreateTimeLessThanOrEqualTo(date);
        example.setOrderByClause("create_time asc");
        List<UserInfo> userInfoList = userInfoMapper.selectByExample(example);
        Map<String, Integer> maps = new LinkedHashMap<>();
        for (UserInfo userInfo : userInfoList) {
            java.sql.Date sqlDate = new java.sql.Date(userInfo.getCreateTime().getTime());
            if (null == maps.get(sqlDate.toString())) {
                maps.put(sqlDate.toString(), 1);
            } else {
                maps.put(sqlDate.toString(), maps.get(sqlDate.toString()) + 1);
            }
        }
        return maps;
    }

    @Override
    public Map<Integer, Integer> getAgeOfAllUser() {
        Integer curYear = new Date().getYear();
        Map<Integer, Integer> map = new HashMap<>();
        List<UserInfo> allUserList = userInfoMapper.selectByExample(new UserInfoExample());
        for (UserInfo userInfo : allUserList) {
            if (userInfo.getUserBirth() != null) {
                int age = curYear - userInfo.getUserBirth().getYear();
                if (null == map.get(age)) {
                    map.put(age, 1);
                } else {
                    map.put(age, map.get(age) + 1);
                }
            }
        }
        Map linkedHashMap = ConnectionUtil.sortMap(map);
        return linkedHashMap;
    }

    @Override
    public Map<String, Integer> getAareOfAllUser() {
        Map<String, Integer> map = new HashMap<>();
        List<UserInfo> userInfoList = userInfoMapper.selectByExample(new UserInfoExample());
        for (UserInfo userInfo : userInfoList) {
            if (userInfo.getUserCity() != null && userInfo.getUserCity().length() > 0) {
                if (null == map.get(userInfo.getUserCity())) {
                    map.put(userInfo.getUserCity(), 1);
                } else {
                    map.put(userInfo.getUserCity(), map.get(userInfo.getUserCity()) + 1);
                }
            }

        }
        return map;
    }

    @Override
    public Map<String, Integer> analySexNumOfUser() {
        Map<String, Integer> map = new HashMap<>();
        List<UserInfo> userInfoList = userInfoMapper.selectByExample(new UserInfoExample());
        for (UserInfo userInfo : userInfoList) {
            if (userInfo.getUserSex() != null && userInfo.getUserSex().length() > 0) {
                if (null == map.get(userInfo.getUserSex())) {
                    map.put(userInfo.getUserSex(), 1);
                } else {
                    map.put(userInfo.getUserSex(), map.get(userInfo.getUserSex()) + 1);
                }
            }
        }
        return map;
    }

    @Override
    public UserInfo login(String username, String password) {
        UserInfoExample userInfoExample = new UserInfoExample();
        userInfoExample.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<UserInfo> userInfoList = userInfoMapper.selectByExample(userInfoExample);
        if (null != userInfoList && userInfoList.size() >= 0) {
            return userInfoList.get(0);
        }
        return null;
    }


}
